import { HealthCheckWithScreenTask, V3proLastErrCache } from "./health.check.class";
import { ScanBoardKey, V3proSubjectMessage } from "../model/v3pro.model";
import { V3proScreen, V3proScreenAllStatus } from "../../../api/v3pro-api/v3pro.api.response.model";
import { V3proAlertMessageErrTypeEnum } from "../model/v3pro.enum";
import { V3proScreenLayoutHelper } from "../helper/v3pro.screen.layout.helper";
import { V3proAllStatusLambda } from "../helper/v3pro.all.status.lambda.helper";
import { V3proCheckScanboardNetworkErrorBitTask } from "./v3pro.check.scanboard.network.error.bit.task";
import { V3proCheckScanboardErrorRestartTask } from "./v3pro.check.scanboard.error.restart.task";
import { V3proFileHelper } from "../helper/v3pro.file.helper";
import { V3proScreenImageHelper } from "../helper/v3pro.screen.image.helper";
import { appConfig } from "../../../configs/env.config.load";
import { V3proCheckPanelCommTask } from "./v3pro.check.panel.comm.task";
import { complement } from "ramda";
import { V3proCheckScanboardVoltageTask } from "./v3pro.check.scanboard.voltage.task";
import { V3proCheckSendboxDoorTask } from "./v3pro.check.sendbox.door.task";

/**
 * 扫描板通信故障
 *
 * 是否支持阈值设置： N
 *
 * 查询周期:
 * 定时查询
 * 间隔1秒
 * 持续30秒
 * 连续5次失败
 *
 * 告警产生的原因:
 * 所有UDP包通信无响应
 * 扫描板、转接板或网线有问题、箱体供电异常
 *
 * 异常现象:
 * 机箱显示黑屏
 * 机箱显示常亮
 * 机箱显示暗红
 * 机箱亮度色彩无法控制
 * 机箱显示画面冻住
 *
 * 维修建议:
 * 依次排查网线、转接板、扫描板、电源等硬件
 *
 * 告警级别: 次要
 */
const errCache = new V3proLastErrCache<ScanBoardKey>();
export class V3proCheckScanboardCommTask extends HealthCheckWithScreenTask<ScanBoardKey[]> {
  async checkResult(
    keyParams: ScanBoardKey[],
    screenId: string,
    allStatus: V3proScreenAllStatus,
    screenLayoutHelper: V3proScreenLayoutHelper,
  ): Promise<V3proSubjectMessage> {
    const message = new V3proSubjectMessage();
    if (keyParams.length == 0) return message;
    message.setErrStatus();
    keyParams.forEach((key) => screenLayoutHelper.setScanBoardErrById(key.id));
    message.files = [
      await V3proFileHelper.scanBoardKey2File(keyParams),
      await new V3proScreenImageHelper(screenLayoutHelper, ["scanBoard"])
        .draw()
        .export2AlertMessageFile(),
    ];
    return message;
  }

  async genKeyParams(
    allStatus: V3proScreenAllStatus,
    screen: V3proScreen,
    screenLayoutHelper: V3proScreenLayoutHelper,
  ): Promise<ScanBoardKey[]> {
    const [errs, normals] = V3proAllStatusLambda.filterScanBoardsWithRemove(
      allStatus,
      complement(V3proAllStatusLambda.communicationOK),
    );

    this.__startChildTasks(
      new V3proCheckScanboardVoltageTask(allStatus, screenLayoutHelper.clone()),
      new V3proCheckPanelCommTask(allStatus, screenLayoutHelper.clone()),
      new V3proCheckScanboardErrorRestartTask(allStatus, screenLayoutHelper.clone()),
      new V3proCheckScanboardNetworkErrorBitTask(allStatus, screenLayoutHelper.clone()),
      new V3proCheckSendboxDoorTask(allStatus, screenLayoutHelper.clone()),
    );
    return errCache.mergeCache(
      errs.map(ScanBoardKey.build),
      normals.map(ScanBoardKey.build),
      screen.id,
      (s) => s.id,
    );
  }

  genSRMqttTopic(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): string {
    return `notify/alert/ssads/device-led-disp:disp-module-fault/${screenId}`;
  }

  getV3proAlertMessage(): V3proAlertMessageErrTypeEnum {
    return V3proAlertMessageErrTypeEnum.SCAN_BOARD_COMM;
  }

  isNeedUploadFile(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): boolean {
    return message.isErrStatus();
  }

  isReportEnabled(keyParams: any, screenId: string): boolean {
    return appConfig.v3pro.errEnable.scanBoardComm;
  }
}
